﻿using System;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;

namespace Model
{
	public class CableTrayBatchCode : HYCmdEntry
	{
		public CableTrayBatchCode(ExternalCommandData commandData) : base(commandData)
		{
		}

		public bool Excute()
		{
			bool result;
			using (DlgCableTrayBatchCode dlgCableTrayBatchCode = new DlgCableTrayBatchCode(this.m_uiDoc))
			{
				Transaction transaction = new Transaction(this.m_doc, "BatchCode");
				transaction.Start();
				if (dlgCableTrayBatchCode.ShowDialog() == DialogResult.OK)
				{
					CableTrayBatchCodeMode mode = CableTrayBatchCodeMode.BatchSel;
					decimal index = -1m;
					if (dlgCableTrayBatchCode.rdoBatch.Checked)
					{
						mode = CableTrayBatchCodeMode.BatchSel;
					}
					else if (dlgCableTrayBatchCode.rdoConnection.Checked)
					{
						mode = CableTrayBatchCodeMode.Connection;
					}
					else if (dlgCableTrayBatchCode.rdoAll.Checked)
					{
						mode = CableTrayBatchCodeMode.All;
					}
					if (dlgCableTrayBatchCode.chkDigitalCode.Checked)
					{
						index = dlgCableTrayBatchCode.nudSequence.Value;
					}
					try
					{
						new CableTrayBatchCodeHelper(this.m_uiDoc, dlgCableTrayBatchCode.txtCableTrayCode.Text.Trim(), mode, dlgCableTrayBatchCode.chkLevel.Checked, dlgCableTrayBatchCode.chkDirection.Checked, index).CableTrayBatchCode();
						transaction.Commit();
						return true;
					}
					catch (Autodesk.Revit.Exceptions.OperationCanceledException)
					{
						transaction.RollBack();
						return false;
					}
				}
				transaction.RollBack();
				result = false;
			}
			return result;
		}
	}
}
